package com.jason.quectel0805.activities;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jason.quectel0805.R;
import com.jason.quectel0805.db.JasonSQLiteOpenHelper;
import com.jason.quectel0805.entities.User;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class JasonSqliteActivity extends AppCompatActivity {
    private static final String TAG = "jasonwan";

    private String dbName = "quectel";
    private int dbVersion = 1;

    private JasonSQLiteOpenHelper openHelper;
    private SQLiteDatabase readableDatabase;
    private SQLiteDatabase writableDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: 执行了JasonSqliteActivity的onCreate方法");
        setContentView(R.layout.activity_jason_sqlite);

        //    1、创建SQLiteOpenHelper的子类对象
        openHelper = new JasonSQLiteOpenHelper(this, dbName, null, dbVersion);
        readableDatabase = openHelper.getReadableDatabase();
        writableDatabase = openHelper.getWritableDatabase();

    }

    /**
     * 利用sqlite保存数据
     *
     * @param view
     */
    public void saveData(View view) {
        //   利用writableDatabase的exeSQL()执行保存数据的sql语句
        //    create table if not exists user(id integer primary key,name varchar(50),age integer,sex varchat(50))
        //writableDatabase.execSQL("insert into user(name,age,sex) values('贲向铭',17,'女')");
        writableDatabase.execSQL("insert into user(name,age,sex) values('Jason',18,'男')");
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

    }

    /**
     * 利用sqlite读取数据
     *
     * @param view
     */
    public void readData(View view) {
        try {
            //利用readableDatabase读取数据库里面的数据
            String selectSql = "select * from user";
            Cursor cursor = readableDatabase.rawQuery(selectSql, null);
            //获取查询结果的数目
            int count = cursor.getCount();
            //创建一个集合，用于存放查询出来的所有对象数据
            List<User> userList = new ArrayList<>();
            while (cursor.moveToNext()) {
                long id = cursor.getLong(0);
                String name = cursor.getString(1);
                int age = cursor.getInt(2);
                String sex = cursor.getString(3);

                User user = new User();
                user.id=id;
                user.sex=sex;
                user.name=name;
                user.age=age;
                userList.add(user);
            }
            Log.d(TAG, "readData: 共" + count + "条数据");
            for (User user : userList) {
                Log.d(TAG, "readData: " + user.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 利用sqlite删除数据
     *
     * @param view
     */
    public void deleteData(View view) {
        writableDatabase.execSQL("delete from user");
    }
}